package ldh.im.admin.service;

import ldh.common.PageResult;
import ldh.im.admin.constant.LogType;
import ldh.im.admin.dao.LogDao;
import ldh.im.admin.pojo.Log;
import ldh.im.admin.pojo.User;
import ldh.im.admin.pojo.where.LogWhere;
import ldh.im.admin.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: ldh
 * @date: 2019-10-25 12:35:05
*/
@Service("logService")
public class LogService {

	@Resource
	private LogDao logDao;

	@Transactional
	public Log insert(User managerUser, User user, LogType logType, String content) {
		Log log = new Log();
		log.setContent(content);
		log.setManagerUser(managerUser);
		log.setUser(user);
		log.setType(logType);
		Date now = new Date();
		log.setCreateTime(now);
		log.setIp(UserUtil.getIp());
		int n = logDao.insert(log);
		if (n != 1) {
			throw new RuntimeException("Log insert error!");
		}
		return log;
	}

	@Transactional
	public Log insert(Log log) {
		int n = logDao.insert(log);
		if (n != 1) {
			throw new RuntimeException("Log insert error!");
		}
		return log;
	}
	
	@Transactional
	public Log insertSelective(Log log) {
		int n = logDao.insert(log);
		if (n != 1) {
			throw new RuntimeException("Log insertSelective error!");
		}
		return log;
	}

	@Transactional
	public Log updateById(Log log) {
		int  n = logDao.updateById(log);
		if (n != 1) {
			throw new RuntimeException("Log update error!");
		}
        return log;
     }

	@Transactional
	public Log updateNotNullById(Log log) {
		int  n = logDao.updateNotNullById(log);
		if (n != 1) {
			throw new RuntimeException("Log update error!");
		}
        return log;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = logDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("Log delete error!");
		}
    }

	@Transactional(readOnly = true)
	public Log getById(Long id) {
		Log log = logDao.getById(id);
		return log;
    }
    
	
	@Transactional(readOnly = true)
	public PageResult<Log> findByLogWhere(LogWhere logWhere) {
		long total = logDao.findTotalByLogWhere(logWhere);
		List<Log> imageList = new ArrayList<Log>();
		if (total > 0) {
			imageList  = logDao.findByLogWhere(logWhere);
		}
		
		return new PageResult<Log>(logWhere, total, imageList);
	}
	
	
}
